package com.xinqing.test.algorithm.dubbo;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.google.gson.Gson;
import com.xinqing.test.modle.algorithm.NewModelParam;
import com.xinqing.test.modle.algorithm.UpdateModelParam;
import com.xinqing.test.modle.algorithm.UpdateModelParamDubbo;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:CHQ
 * @Date:2020/9/28 10:20
 * @Description
 */
public class UpdateModel {
    public static void main(String args[]) throws Exception {
        NewModelParam param= new NewModelParam();

        Map<String, String[]> map = new HashMap<String, String[]>();
        map.put("c0", new String[]{"string", "0"});
        map.put("c1", new String[]{"string", "0"});
        map.put("c2", new String[]{"string", "0"});
        map.put("c3", new String[]{"string", "0"});
        map.put("c4", new String[]{"string", "0"});
        map.put("c5", new String[]{"string", "0"});
        map.put("c6", new String[]{"string", "0"});
        map.put("c7", new String[]{"string", "0"});
        map.put("c8", new String[]{"sequence", "0"});
        map.put("c9", new String[]{"sequence", "0"});
        map.put("u5", new String[]{"string", "0"});
        map.put("u7", new String[]{"string", "0"});
        map.put("u8", new String[]{"string", "0"});
        map.put("u11", new String[]{"double", "0"});
        map.put("u17", new String[]{"string", "0"});
        map.put("u18", new String[]{"string", "0"});
        map.put("u19", new String[]{"string", "0"});
        map.put("u23", new String[]{"string", "0"});
        map.put("u26", new String[]{"string", "0"});
        map.put("u28", new String[]{"string", "0"});
        map.put("u29", new String[]{"string", "0"});
        map.put("u31", new String[]{"string", "0"});
        map.put("u32", new String[]{"string", "0"});
        map.put("u33", new String[]{"string", "0"});
        map.put("u34", new String[]{"string", "0"});
        map.put("u36", new String[]{"double", "0"});
        map.put("u37", new String[]{"double", "0"});
        map.put("u38", new String[]{"double", "0"});
        map.put("u39", new String[]{"double", "0"});
        map.put("u40", new String[]{"double", "0"});
        map.put("u41", new String[]{"double", "0"});
        map.put("u42", new String[]{"double", "0"});
        map.put("u43", new String[]{"string", "0"});
        map.put("u44", new String[]{"double", "0"});
        map.put("u45", new String[]{"double", "0"});
        map.put("us0", new String[]{"double", "0"});
        map.put("us1", new String[]{"double", "0"});
        map.put("us2", new String[]{"double", "0"});
        map.put("us3", new String[]{"string", "0"});
        map.put("us4", new String[]{"string", "0"});
        map.put("n1", new String[]{"double", "0"});
        map.put("n2", new String[]{"double", "0"});
        map.put("n3", new String[]{"double", "0"});
        map.put("n4", new String[]{"double", "0"});
        map.put("n5", new String[]{"double", "0"});
        map.put("n6", new String[]{"double", "0"});
        map.put("n7", new String[]{"double", "0"});
        map.put("n8", new String[]{"double", "0"});
        map.put("n9", new String[]{"double", "0"});
        map.put("n10", new String[]{"double", "0"});
        map.put("uc1", new String[]{"sequence", "0"});
        param.setToken("951c327c56779183");
        param.setSource("manual");
        param.setPoolSize(0);
        param.setModelTypes("tensorflow");
        param.setHdfsPath("ssj_wdl_emb_v2:8112");
        param.setDescription("ssj_wdl_emb_v2模型");
        param.setCreator("chq");
        param.setFieldInfos(JSON.toJSONString(map));

        String data = JSON.toJSONString(param);

        //分配的AccessKeyId
        String accessKeyId = "data_team";
        //分配的AccessSecret
        String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";

        String url = "http://sml.suishoudata.com/sml/model/push";
        //调用鉴权工具包计算鉴权信息,data不存则传空
        AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
        //获取GMT时间+
        String date = authInfo.getDate();
        //获取加密之后的data，若传入data为空，则密文也为空
        String ciphertext = authInfo.getCiphertext();
        //获取认证信息
        String authorization = authInfo.getAuthorization();

        //构造请求
        HttpPost method = null;
        try {
            method = new HttpPost(url);
            method.setHeader("Date", date);
            method.setHeader("Authorization", authorization);
            method.setHeader("AuthVersion", "2");
            if (ciphertext != null) {
                //加密之后data通过http body传输
                StringEntity stringEntity = new StringEntity(ciphertext, Consts.UTF_8);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                method.setEntity(stringEntity);
            }
            CustomConfiguration.setString("http.connection.socket.timeout", "50000");
            StringResponse response = HttpUtil.post(method);
            System.out.println("rslt:" + response.getResponseBody());
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
    }
}


